﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gallery.Models;

namespace Gallery.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            var db = new GalleryModelContainer();
            var d = (from p in db.Albums select p).ToList();

            return View(d);
        }

        public ActionResult Albumdetail()
        {
            int id = Int32.Parse(Request["id"].ToString());
            var db = new GalleryModelContainer();
            var album = (from p in db.Albums where p.Id == id select p).FirstOrDefault();
            ViewBag.id = id;
            ViewBag.name = album.Name;
            ViewBag.description = album.Description;

            var albums = (from p in db.Albums select p).ToList();
            ViewBag.albums = albums;
            var pics = (from p in db.Pictures where p.AlbumId == id select p).ToList();

            return View(pics);
        }

        public ActionResult Content()
        {
            ViewBag.Message = "Your contact page.";

            int id = Int32.Parse(Request["id"].ToString());
            var db = new GalleryModelContainer();
            var pic = (from p in db.Pictures where p.Id == id select p).FirstOrDefault();

            var album = (from p in db.Albums where p.Id == pic.AlbumId select p).FirstOrDefault();
            ViewBag.albumname = album.Name;

            var albums = (from p in db.Albums select p).ToList();
            ViewBag.albums = albums;

            var pics = (from p in db.Pictures where p.AlbumId == pic.AlbumId select p).ToList();
            
            ViewBag.pictures = pics;
            return View(pic);
        }

        public ActionResult Createeditalbum()
        {
            int id = 0;
            try
            {
                id = Int32.Parse(Request["id"].ToString());
            }
            catch (Exception ex)
            { 
                
            }
            
            var db = new GalleryModelContainer();
            var albums = (from p in db.Albums select p).ToList();
            ViewBag.albums = albums; 
            if (id != 0)
            {             
                var d = (from p in db.Albums where p.Id == id select p).FirstOrDefault();
                return View(d);
            }
            return View();
        }

        public ActionResult Createalbum(int id, string name, string description, HttpPostedFileBase avatar)
        {
            if (id == 0)
            {
                var album = new Album();
                album.Name = name;
                if (description != null)
                    album.Description = description;
                album.UserId = 1;
                string fileName = null;
                var db = new GalleryModelContainer();
                db.Albums.Add(album);
                db.SaveChanges();
                if (avatar != null && avatar.ContentLength > 0)
                {
                    // extract only the fielname
                    fileName = Path.GetFileName(album.Id.ToString() + "-" + avatar.FileName);
                    // store the file inside ~/App_Data/uploads folder
                    var path = Path.Combine(Server.MapPath("~/Images/Albums/Avatars"), fileName);

                    avatar.SaveAs(path);
                    album.Avatar = fileName;
                    db.SaveChanges();
                }
                return RedirectToAction("Index", "Home");
            }
            else
            {
                var db = new GalleryModelContainer();
                var album = (from p in db.Albums where p.Id == id select p).FirstOrDefault();
                album.Name = name;
                album.Description = description;
                string fileName = null;
                if (avatar != null && avatar.ContentLength > 0)
                {
                    
                    var oldfile = Server.MapPath("~/Images/Albums/Avatars/"+ album.Avatar);
                    System.IO.File.Delete(oldfile);
                    // extract only the fielname
                    fileName = Path.GetFileName(album.Id.ToString() + "-" + avatar.FileName);
                    // store the file inside ~/App_Data/uploads folder
                    var path = Path.Combine(Server.MapPath("~/Images/Albums/Avatars"), fileName);

                    avatar.SaveAs(path);
                    album.Avatar = fileName;
                    
                }
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
        }
    }
}
